package com.hxd.otc.other;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;

import com.hxd.otc.R;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.widget.BlocksToast;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;


/**
 * 项  目 :  BlockNews
 * 包  名 :  cn.weiqitonggame.blocknews.utils
 * 类  名 :  UMengSharePicker
 * 作  者 :  胡庆岭
 * 时  间 :  2018/4/11 14:21
 * 描  述 :  ${TODO}友盟工具类分享和登录
 */
public class UMengSharePicker implements UMShareListener {
    @SuppressLint("StaticFieldLeak")
    //private static UMengSharePicker mUMengSharePicker;
    private Activity mActivity;
    private static boolean mIsCustom;
    private static SHARE_MEDIA[] mMedia;

    private UMengSharePicker(@NonNull Activity activity) {
        this.mActivity = DataUtils.checkNull(activity);
    }

    /*private static UMengSharePicker getInstance(@NonNull Activity activity) {
        synchronized (UMengSharePicker.class) {
            if (mUMengSharePicker == null) {
                mUMengSharePicker = new UMengSharePicker(activity);
            }
        }
        return mUMengSharePicker;
    }*/

    /**
     * 分享文本
     *
     * @param data 文本
     */
    public void shareText(@NonNull String data) {

        ShareAction shareAction = new ShareAction(mActivity).withText(data).setCallback(this);
        this.get(shareAction);
    }

    /**
     * 分享图片
     *
     * @param url 图片url
     */
    public void shareImage(String url) {
        ShareAction shareAction = new ShareAction(mActivity).withMedia(new UMImage(mActivity, url)).setCallback(this);
        this.get(shareAction);

    }

    /**
     * 分享图片
     *
     * @param imageRes 图片资源
     */
    public void shareImage(@DrawableRes int imageRes) {
        UMImage umImage = new UMImage(mActivity, imageRes);
        umImage.setThumb(umImage);
        ShareAction shareAction = new ShareAction(mActivity).withMedia(umImage).setCallback(this);
        this.get(shareAction);

    }

    /**
     * 分享图片
     *
     * @param bitmap bitmap
     */
    public void shareImage(@NonNull Bitmap bitmap) {
        UMImage umImage = new UMImage(mActivity, bitmap);
        umImage.setThumb(umImage);
        ShareAction shareAction = new ShareAction(mActivity).withMedia(umImage).setCallback(this);
        this.get(shareAction);

    }


    /**
     * 分享图片
     *
     * @param file bitmap
     */
    public void shareImage(@NonNull File file) {
        UMImage umImage = new UMImage(mActivity, file);
        umImage.setThumb(umImage);
        ShareAction shareAction = new ShareAction(mActivity).withMedia(umImage).setCallback(this);
        this.get(shareAction);

    }


    /**
     * 分享一个链接
     *
     * @param umWeb bean
     */
    public void shareLink(@NonNull UMWeb umWeb) {

        LogUtils.w("shareLink", UMShareAPI.get(mActivity).isSupport(mActivity, SHARE_MEDIA.WEIXIN) + "--" + UMShareAPI.get(mActivity).isAuthorize(mActivity, SHARE_MEDIA.WEIXIN) + "--" + UMShareAPI.get(mActivity).isInstall(mActivity, SHARE_MEDIA.WEIXIN));
        ShareAction shareAction = new ShareAction(mActivity).withMedia(umWeb).setCallback(this);
        this.get(shareAction);
    }


    private void get(@NonNull ShareAction shareAction) {
        if (mIsCustom) {
            shareAction.setPlatform(mMedia[0]);
            shareAction.share();
        } else {
            shareAction.setDisplayList(mMedia);
            shareAction.open();
        }
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {
        LogUtils.w("UMShare--", share_media.getName());
    }


    @Override
    public void onResult(SHARE_MEDIA share_media) {
        LogUtils.w("UMShare--", share_media.getName() + "---" + Thread.currentThread().getName());
        BlocksToast.get().showToast(R.string.share_succeed);

    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        BlocksToast.get().showToast(R.string.share_fail);
        LogUtils.w("UMShare--", share_media.getName() + throwable.getMessage());
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        BlocksToast.get().showToast(R.string.share_cancel);
        LogUtils.w("UMShare--", share_media.getName());
    }

    /**
     * 构造分享
     */
    public static class Builder {

        public Builder setPlatforms(@NonNull SHARE_MEDIA... media) {
            UMengSharePicker.mMedia = media;
            return this;

        }

        public Builder isCustomShare(boolean isCustom) {
            UMengSharePicker.mIsCustom = isCustom;
            return this;

        }

        public UMengSharePicker builder(@NonNull Activity activity) {
            return new UMengSharePicker(activity);
        }
    }

    /**
     * 分享监听
     */
    /*public class UMShare implements UMShareListener {

        @Override
        public void onStart(SHARE_MEDIA share_media) {
            LogUtils.w("UMShare--", share_media.getName());
        }


        @Override
        public void onResult(SHARE_MEDIA share_media) {
            LogUtils.w("UMShare--", share_media.getName()+"---"+Thread.currentThread().getName());
            BlocksToast.get().showToast(R.string.share_succeed);

        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            BlocksToast.get().showToast(R.string.share_fail);
            LogUtils.w("UMShare--", share_media.getName() + throwable.getMessage());
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            BlocksToast.get().showToast(R.string.share_cancel);
            LogUtils.w("UMShare--", share_media.getName());
        }
    }*/
}
